<template>
  <div class="page">

    <!-- 搜索区域 开始 -->
    <div class="el_card_box">
      <el-form :inline="true" size="small" :model="formInline" class="search_form">
        <el-form-item label="部门：">
          <depart-ment v-model="formInline.departmentIds"></depart-ment>
        </el-form-item>
        <el-form-item label="城市：">
          <city-select v-model="formInline.cities" :multiple="false"></city-select>
        </el-form-item>
        <el-form-item label="关键词：">
          <el-input v-model="formInline.term" placeholder="请输入客户电话/姓名"></el-input>
        </el-form-item>
      </el-form>
      <div class="search_btn_box">
        <el-button type="primary" size="small" @click="getList(1)">筛选</el-button>
        <el-button type="default" size="small" @click="rest">清空筛选</el-button>
      </div>
    </div>
    <!-- 搜索区域 end -->

    <!-- 表格 开始 -->
    <div class="table_box">
      <!-- 操作区域 开始 -->
      <div class="action_box">
        <el-button type="primary" size="small" @click="openDialog()">新增分配人</el-button>
        <el-button type="default" size="small" @click="restRate()">重置增减比例</el-button>
      </div>
      <!-- 操作区域 end -->

      <el-table ref="elTable" class="el_table" size="small" :data="tableData" v-loading="loading">
        <el-table-column prop="departmentName" label="所属部门" show-overflow-tooltip />
        <el-table-column prop="username" label="用户名称" show-overflow-tooltip />
        <el-table-column prop="account" label="手机号码" show-overflow-tooltip />
        <el-table-column prop="assignOrder" label="排序" show-overflow-tooltip />
        <el-table-column prop="assignCities" label="可分配城市" show-overflow-tooltip />
        <el-table-column prop="enableStatus" label="是否分配" show-overflow-tooltip>
          <template slot-scope="{row}">
            <el-switch v-model="row.enableStatus" :active-value="1" :inactive-value="2" @change="e => onChange(e,row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="indecreaseRatio" label="线索增减比例" show-overflow-tooltip />
        <el-table-column prop="address" label="操作" width="120" align="center" fixed="right">
          <template slot-scope="{row}">
            <el-link type="primary" class="el_link" :id="row.id" @click="openDialog(row.id)">编辑</el-link>
            <el-link type="danger" class="el_link" @click="remove(row)">删除</el-link>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination_list">
        <el-pagination background layout="sizes, total,prev, pager, next, jumper" :total="pageTotal" :page-size="pageSize" :page-sizes="pageSizes" :current-page.sync="pageNumber" @current-change="getList" @size-change="changePageSize" />
      </div>
    </div>
    <!-- 表格 end -->

    <!-- 新增分配人弹框 开始 -->
    <add-assigner v-model="showDialog" v-if="showDialog" :id="currentId" @change="getList(1)"></add-assigner>
    <!-- 新增分配人弹框 end -->

  </div>
</template>

<script>
import { showModal, showToast, deepClone } from "@/common/util";
import {
  GET_ASSIGNMENT_SEARCH,
  DELETE_ASSIGNMENT_INFO,
  UPDATE_ASSIGNMENT_STATUS,
  REST_ASSIGNMENT_RATE,
} from "@/http/clues";
import AddAssigner from "./addAssigner.vue";
export default {
  components: {
    AddAssigner,
  },
  data() {
    return {
      formInline: {},
      tableData: [],
      pageTotal: 0,
      pageNumber: 1,
      pageSizes: [10, 20, 50, 100],
      pageSize: 20,
      loading: false,
      showDialog: false,
      currentId: "",
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    /** 获取用户列表 */
    getList(current) {
      let _this = this;
      let oParams = _this.getParams();
      oParams.pageNumber = current || _this.pageNumber;
      console.log(oParams);
      _this.loading = true;
      GET_ASSIGNMENT_SEARCH(oParams).then((res) => {
        console.log(res);
        _this.loading = false;
        let obj = res.data || {};
        _this.tableData = obj.list || [];
        _this.pageTotal = obj.totalElements;
      });
    },
    /** 每页展示 */
    changePageSize(e) {
      this.pageSize = e;
      this.getList();
    },

    /** 获取参数 */
    getParams() {
      let o = JSON.parse(JSON.stringify(this.formInline));
      let m = o.orderTime || [];
      let n = o.orderTime2 || [];
      if (m && m.length > 0) {
        o.orderStartTime = m[0];
        o.orderEndTime = m[1];
      }

      if (n && n.length > 0) {
        o.feedbackStartTime = n[0];
        o.feedbackEndTime = n[1];
      }

      delete o.orderTime;
      delete o.orderTime2;

      o.pageSize = this.pageSize;
      return o;
    },

    /** 重置 */
    rest() {
      this.formInline = {};
      this.pageNumber = 1;
      this.getList();
    },

    // 打开弹框
    openDialog(id) {
      this.showDialog = true;
      if (id) {
        this.currentId = id;
      }
    },

    // 删除
    remove(row) {
      let _this = this;
      showModal({
        message: "确定删除该条数据吗？",
        type: "warning",
        showCancelButton: true,
        success: function () {
          DELETE_ASSIGNMENT_INFO({ id: row.id }).then((res) => {
            if (res.code == 200) {
              showToast({
                message: "删除成功",
                type: "success",
              });
              _this.getList();
            }
          });
        },
      });
    },

    // 启停
    onChange(e, row) {
      UPDATE_ASSIGNMENT_STATUS({ id: row.id }).then((res) => {
        if (res.code == 200) {
          showToast({
            message: "操作成功",
            type: "success",
          });
        }
      });
    },

    // 重置比例
    restRate() {
      let _this = this;
      showModal({
        message: "确认重置增减比例吗？",
        type: "warning",
        showCancelButton: true,
        success: function () {
          REST_ASSIGNMENT_RATE().then((res) => {
            if (res.code == 200) {
              showToast({
                message: "操作成功",
                type: "success",
              });
              _this.getList();
            }
          });
        },
      });
    },
  },
};
</script>

<style lang="scss" scoped>
</style>
